<?php

/*
 * This class is used for common same function.
 */

class Model_Company extends Model_Base {

    protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->COMPANY_TBL;
        $this->_primaryKey = 'comp_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	public function getCompanycurrencyArray() {
        return array(
            "" => "-- Select Company Currency --",
            "us_dollar" => "US Dollar",
            "euro" => "Euro"
        );
    }
	
	public function getCompanyindustryArray() {
        return array(
            "" => "",
            "industry" => "Industry",
            "academia" => "Academia",
			"r&d institute" => "R&D Institute",
            "consulting" => "Consulting",
			"service_provider" => "Service Provider"
        );
    }
	
	public function getCompanytypeArray() {
        return array(
            "" => "",
            "client" => "Client",
            "partner" => "Partner",
			"reseller" => "Reseller",
            "competitor" => "Competitor",
			"investor" => "Investor",
            "integrator" => "Integrator",
			"prospect" => "Prospect",
            "media" => "Media",
			"other" => "Other"
        );
    }
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('cmp' => $this->COMPANY_TBL))
                ->where('comp_id = ?', $id)
				->where("comp_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchCompanyData($where = "", $limit = "", $offset = "0", $order = "comp_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("cmp" => $this->COMPANY_TBL))
				->setIntegrityCheck(false)
				->join(array('lgn' => 'tbl_login'), "cmp.comp_responsible_person = lgn.lgn_id")
				->where("comp_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	public function getCompanyCount($where = "") {
        $select = $this->select()
                ->from(array("cmp" => $this->COMPANY_TBL), Array('cnt' => 'count(comp_id)'))
			    ->where("comp_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function  fetchCompany($where = ""){
        
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array("cmp" => $this->COMPANY_TBL))
                ->where("comp_status != 2");
		if ($where != '') {
            $select->where($where);
        }
		
        $result = $this->fetchAll($select);
		//echo $select;
		
        $finalArray[''] = '';
        foreach ($result as $res) {
            
            $finalArray[$res['comp_id']] = $res['comp_name'];
        }
		
        return $finalArray;
		
    }
	
	public function deleteCompany($companyArray) {
        if (is_array($companyArray)) {
            foreach ($companyArray as $_cmp) {
                $companyData = Array();
                $companyData = $this->fetchEntryById($_cmp);
                if (count($companyData) > 0) {
                    $data = array("comp_status" => "2");
                    $this->update($data, "comp_id =" . (int) $_cmp);
                }
            }
        } else {
            $comp_id = $companyArray;
            $companyData = $this->fetchEntryById($comp_id);
            if (count($companyData) > 0) {
                $data = array("comp_status" => "2");
                $this->update($data, "comp_id =" . $comp_id);
            }
        }
    }
}